package els;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;

import com.bridge.domain.PropertyVO;
import com.els.ElsConnection;
import com.els.factory.ElasticFactory;
import com.google.gson.JsonObject;

public class ElsPOC {

	@Test
	public void test() {
		ElsConnection connection= ElasticFactory.getConnection();
		QueryBuilder queryBuilder=QueryBuilders.matchAllQuery();

		SearchRequestBuilder request = connection.prepareSearch("tjws", "tjws", queryBuilder);
		SearchResponse response = request.execute().actionGet();
		System.out.println(response);
	}
	@Test
	public void create(){
		ElsConnection ec=ElasticFactory.getConnection();
		ec.newIndex("system", 5, 1);
		ec.newIndex("log", 5, 1);
	}
	@Test
	public void mapping(){
		ElsConnection ec=ElasticFactory.getConnection();
		List<PropertyVO> mappings=new ArrayList<PropertyVO>();
		//所属模块
		PropertyVO p1=new PropertyVO();
		p1.setName("module");
		p1.setType("string");
		p1.setPpl(false);
		p1.setRaw(false);
		mappings.add(p1);
		//检查时间
		PropertyVO p2=new PropertyVO();
		p2.setName("time");
		p2.setType("date yyyy-MM-dd HH:mm:ss");
		p2.setPpl(false);
		p2.setRaw(false);
		mappings.add(p2);
		//是否正常
		PropertyVO p3=new PropertyVO();
		p3.setName("run");
		p3.setType("boolean");
		p3.setPpl(false);
		p3.setRaw(false);
		mappings.add(p3);
		//备注
		PropertyVO p4=new PropertyVO();
		p4.setName("remark");
		p4.setType("string");
		p4.setPpl(true);
		p4.setRaw(true);
		mappings.add(p4);
		//子模块
		PropertyVO p5=new PropertyVO();
		p5.setName("sub_module");
		p5.setType("string");
		p5.setPpl(false);
		p5.setRaw(false);
		mappings.add(p5);
		
		ec.newType("log", "log", mappings);
	}
	@Test
	public void insert(){
		ElsConnection ec=ElasticFactory.getConnection();
		JsonObject obj=new JsonObject();
		obj.addProperty("name", "Li.Yang");
		obj.addProperty("tel", "18368181525");
		obj.addProperty("role", "customer");
		obj.addProperty("active", true);
		ec.insertOne("system", "notice", null, obj);
	}
}
